package com.ruoyi.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ruoyi.service.PeopleAndPhotoInterfaceService;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PeopleAndPhotoInterfaceServiceImpl implements PeopleAndPhotoInterfaceService {
    /**
     * 能力开放平台的网站路径
     * TODO 路径不用修改，就是/artemis
     */
    private static final String ARTEMIS_PATH = "/artemis";

    /**
     * host = "10.2.148.81";// 代理API网关nginx服务器ip端口
     * appKey = "20917555";// 秘钥appkey
     * appSecret = "UpxFUpGEKrXUFpgsTF8i";// 秘钥appSecret
     */
    private static ArtemisConfig artemisConfig = new ArtemisConfig("10.40.9.20:8001", "artemis-portal", "1550445409803960320");

    @Resource
    PeopleAndPhotoInterfaceService peopleAndPhotoInterfaceService;

    @Override
    public Object savePersonnel(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/single/add";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object updatePersonnel(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/single/update";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object listOrganizationalStaff(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/orgIndexCode/personList";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object listStaff(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/personList";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object selectStaffList(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v2/person/advance/personList";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object removeStaff(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/batch/delete";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object updateFace(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/face/single/update";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object saveFace(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/face/single/add";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object removeFaceById(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/face/single/delete";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object saveListPersonnel(List<Add> addList) throws Exception {
        String addDataApi = ARTEMIS_PATH + "/api/resource/v1/person/batch/add";
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", addDataApi);
            }
        };
        String body = JSON.toJSONString(addList);
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object downloadPersonnelImage(JSONObject jsonObject) throws Exception {
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/picture";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }

    @Override
    public Object getPersonnelById(JSONObject jsonObject) throws Exception{
        final String getCamsApi = ARTEMIS_PATH + "/api/resource/v1/person/condition/personInfo";// 接口请求地址
        Map<String, String> paramMap = new HashMap<String, String>();// post请求Form表单参数
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            paramMap.put(entry.getKey(), JSON.toJSONString(entry.getValue()));
        }
        String body = JSON.toJSON(paramMap).toString();
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", getCamsApi);
            }
        };
        return ArtemisHttpUtil.doPostStringArtemis(artemisConfig, path, body, null, null, "application/json");
    }
}
